class newHook {
    constructor(beforeHooks = {}, afterHooks = {}) {
        this.XHR = window.XMLHttpRequest
        this.beforeHooks = beforeHooks
        this.afterHooks = afterHooks
    }
    init() {
        let _this = this
        _this.xhr = new this.XHR()
        this.overWrite(_this)
    }
    overWrite(proxyXHR) {
        for(let key of proxyXHR.xhr){
            // 判断里面的属性和方法， 如果是方法
            if(typeof proxyXHR.xhr[key] === 'function'){
                this.overwriteMethod(key, proxyXHR)
                continue;
            }
            // 如果是属性值
            this.overwriteAttribute(key, proxyXHR)
        }
    }
    overwriteMethod(key, proxyXHR){
        let beforeHooks = this.beforeHooks;
        let afterHooks = this.afterHooks;
        proxyXHR[key] = (...args) => {
            if(beforeHooks[key]){
                const res = beforeHooks[key].call(proxyXHR, args)
                if(res === false) return;
            }
        }
        const res = proxyXHR._xhr.apply(proxyXHR._xhr, args)
        afterHooks[key] && afterHooks[key].call(proxyXHR._xhr,res)
        return res;
    }
    overwriteAttribute(key, proxyXHR){
        Object.defineProperties(proxyXHR,this.setPropertyDescriptor(key,proxyXHR))
    }

    //TODO:属性重写改写的方式
    setPropertyDescriptor(key, proxyXHR){

    }
}
